package com.woniu.inlet.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.appcommand.DelAppCommand;
import com.woniu.command.matcommand.AddMatCommand;
import com.woniu.command.matcommand.ReadyCommand;
import com.woniu.command.matcommand.ReceCommand;
import com.woniu.inlet.vo.MaterialApplicationVo;
import com.woniu.outlet.po.MaterialApplication;
import com.woniu.service.MatApplicationService;
import com.woniu.util.JwtTokenUitl;
import com.woniu.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("MatApplication")
@Api(tags = "请领controller")
public class MatApplicationController {
    @Autowired
    private MatApplicationService matApplicationService;

    @GetMapping("getMatApplications")
    @ApiOperation("查询所有的请领记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "t1",value = "开始时间,模糊查询"),
            @ApiImplicitParam(name = "t2",value = "结束时间,模糊查询"),
    })
    public ResponseResult getMatApplications(Date t1, Date t2) throws JsonProcessingException {
        List<MaterialApplication> materialApplicationList = matApplicationService.getMaterialApplicationList(t1, t2);
        ArrayList<MaterialApplicationVo> materialApplicationVos = new ArrayList<>();
        for (MaterialApplication materialApplication : materialApplicationList) {
            MaterialApplicationVo materialApplicationVo = new MaterialApplicationVo();
            BeanUtils.copyProperties(materialApplication,materialApplicationVo);
            materialApplicationVos.add(materialApplicationVo);
        }
        return new ResponseResult(materialApplicationVos,"查询成功",200);
    }

    @PostMapping("addMatApplication")
    @ApiOperation("添加请领")
    @ApiOperationSupport(ignoreParameters = {"applicationDetails.spare1","deptId","deptName","username","userId","id","applicationCode","applicationTime","spare1","spare2","spare3"})
    public ResponseResult addMatApplication(@RequestBody MaterialApplication materialApplication, HttpServletRequest request){
        String token = request.getHeader("tokenStr");
        String userName = JwtTokenUitl.getUserName(token);
        materialApplication.setUsername(userName);
        Command command = new AddMatCommand();
        command.execute(materialApplication);
        return new ResponseResult(200,"添加请领成功");
    }

    @DeleteMapping("delMatApplication")
    @ApiOperation("删除请领")
    public ResponseResult delMatApplication( Integer id){
        Command command = new DelAppCommand();
        command.execute(id);
        return new ResponseResult(200,"删除请领成功");
    }

    /** 给仓库那边调用,仓库给一个申请编码,可以只查到对应申请单下的物料清单,不能查看到订单和客户那边信息
     * @param applicationCode
     * @return
     */
    @GetMapping("getMatApplicationByCode")
    @ApiOperation("根据请领编码查询对应的物料清单")
    public ResponseResult getMatApplicationByCode( String applicationCode) throws JsonProcessingException {
        MaterialApplication materialApplication = matApplicationService.getMaterialApplicationByCode(applicationCode);
        return new ResponseResult(200,"查询成功");
    }

    @PutMapping("receive")
    @ApiOperation("确认已领取物料")
    public ResponseResult receive(String applicationCode){
        Command command = new ReceCommand();
        command.execute(applicationCode);
        return new ResponseResult(200,"物料已领取");
    }

    /** 修改请领状态为已备齐
     * @param applicationCode
     * @return
     */
    @ApiOperation("给仓库使用的,仓库那边已备货")
    @PutMapping("ready")
    public ResponseResult ready(String applicationCode){
        Command command = new ReadyCommand();
        command.execute(applicationCode);
        return new ResponseResult(200,"仓库已备货");
    }

}